package com.iwayin.web.action.loginwith;

import org.apache.struts2.ServletActionContext;

import com.iwayin.core.bo.loginwith.LoginWithBO;
import com.iwayin.core.common.Constants;
import com.iwayin.core.dataobject.UserDO;
import com.iwayin.core.enumer.LoginWithEnum;
import com.iwayin.web.action.BaseAction;
import com.iwayin.web.common.AjaxResult;
import com.iwayin.web.common.CookieUtil;
import com.iwayin.web.common.ThreadLocalManager;
import com.iwayin.web.common.WebConstants;

public class LoginWithAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = -6305843837411078926L;
	private int type;
	private String code;

	private AjaxResult ajaxResult;

	private LoginWithBO loginWithBO;

	@SuppressWarnings("unchecked")
	public String loginWith() {
		try {
			ajaxResult = AjaxResult.succResultAndInitMap();
			String oauthUrl = null;
			if (type == LoginWithEnum.QQ.getType()) {
				oauthUrl = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=" + LoginWithEnum.QQ.getAppKey()
						+ "&redirect_uri=http://yoasking.com/loginWith/loginWithQQ.action&scope=get_user_info,get_info";
			} else if (type == LoginWithEnum.SINA.getType()) {
				oauthUrl = "https://api.weibo.com/oauth2/authorize?response_type=code&client_id=" + LoginWithEnum.SINA.getAppKey()
						+ "&redirect_uri=http://yoasking.com/loginWith/loginWithSina.action";
			} else if (type == LoginWithEnum.TAOBAO.getType()) {
				oauthUrl = "https://oauth.taobao.com/authorize?response_type=code&client_id=" + LoginWithEnum.TAOBAO.getAppKey()
						+ "&redirect_uri=http://yoasking.com/loginWith/loginWithTb.action&scope=item&view=web";
			} else if(type == LoginWithEnum.RENREN.getType()){
				oauthUrl = "https://graph.renren.com/oauth/authorize?client_id=" + LoginWithEnum.RENREN.getAppId()
						+ "&response_type=code&redirect_uri=http://yoasking.com/loginWith/loginWithRenren.action&display=page";
			}
			ajaxResult.getResultMap().put("oauthUrl", oauthUrl);
		} catch (Exception e) {
			ajaxResult = AjaxResult.errorResult(e.getMessage());
		}
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	public String loginWithQQ() {
		UserDO user = loginWithBO.addQQMember(code);
		session.put(WebConstants.LOGIN_USER, user);
		ThreadLocalManager.setLoginUser(user);
		CookieUtil.addCookie(ServletActionContext.getResponse(), Constants.COOKIE_USERNAME, this.getLoginUser().getNickname());
		CookieUtil.addCookie(ServletActionContext.getResponse(), Constants.COOKIE_PASSWORD, this.getLoginUser().getLoginPwd());
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	public String loginWithTb() {
		UserDO user = loginWithBO.addTbMember(code);
		session.put(WebConstants.LOGIN_USER, user);
		ThreadLocalManager.setLoginUser(user);
		CookieUtil.addCookie(ServletActionContext.getResponse(), Constants.COOKIE_USERNAME, this.getLoginUser().getNickname());
		CookieUtil.addCookie(ServletActionContext.getResponse(), Constants.COOKIE_PASSWORD, this.getLoginUser().getLoginPwd());
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	public String loginWithSina() {
		UserDO user = loginWithBO.addSinaMember(code);
		session.put(WebConstants.LOGIN_USER, user);
		ThreadLocalManager.setLoginUser(user);
		CookieUtil.addCookie(ServletActionContext.getResponse(), Constants.COOKIE_USERNAME, this.getLoginUser().getNickname());
		CookieUtil.addCookie(ServletActionContext.getResponse(), Constants.COOKIE_PASSWORD, this.getLoginUser().getLoginPwd());
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public String loginWithRenren(){
		UserDO user = loginWithBO.addRenrenMember(code);
		session.put(WebConstants.LOGIN_USER, user);
		ThreadLocalManager.setLoginUser(user);
		CookieUtil.addCookie(ServletActionContext.getResponse(), Constants.COOKIE_USERNAME, this.getLoginUser().getNickname());
		CookieUtil.addCookie(ServletActionContext.getResponse(), Constants.COOKIE_PASSWORD, this.getLoginUser().getLoginPwd());
		return SUCCESS;
	}

	public AjaxResult getAjaxResult() {
		return ajaxResult;
	}

	public void setType(int type) {
		this.type = type;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public void setLoginWithBO(LoginWithBO loginWithBO) {
		this.loginWithBO = loginWithBO;
	}
}
